<template>
  <div class="login-container">
    <el-form
      ref="etoak"
      :rules="rules"
      :model="ruleForm"
      label-width="90px"
      class="login-form"
      status-icon
    >
      <h2 class="login-title">天天向上吧</h2>
      <el-form-item label="用户姓名" prop="username">
        <el-input
          v-model="ruleForm.username"
          placeholder="请输入用户姓名"
          clearable=""
        ></el-input>
      </el-form-item>
      <el-form-item label="用户密码" prop="password">
        <el-input
          v-model="ruleForm.password"
          placeholder="请输入用户密码"
          show-password
          clearable=""
        ></el-input>
      </el-form-item>
      <el-form-item class="e-btn">
        <el-button type="primary" size="medium" @click="submitForm('etoak')" round
          >登录</el-button
        >
        <el-button size="medium" @click="register" round type="primary" plain
          >注册</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      title: "登录练习",
      ruleForm: {
        username: "",
        /* 封装用户密码 */
        password: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入用户姓名", trigger: "blur" },
          { min: 0, max: 10, message: "用户姓名在0到10个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          { min: 6, max: 32, message: "用户姓名在6到32个字符", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    /* 提交表单 */
    submitForm(formName) {
      /* 这个formName是形参也就是按钮中函数传递的值，也就是ref的值 */
      this.$refs[formName].validate((valid) => {
        //console.log(valid)
        /* 表单验证成功则valid为true */
        if (valid) {
          this.$dao.login(this.ruleForm.username, this.ruleForm.password).then((response) => {
            // console.log(response.data);
            const resp = response.data;
            /* 用户登录成功 */
            if (resp.flag) {
              /* 获得用户资料 */
              this.$dao.getUserInfo(resp.data.id).then((response) => {
                const respUser = response.data;
                /* 获取用户资料成功 */
                if (respUser.flag) {
                  // 将信息保存到浏览器的 localStorage中
                  localStorage.setItem("etoak_cms_user", JSON.stringify(respUser.data));
                  // 方便后面重新验证
                  /* 
                                            "token": "admin"
                                        */
                  localStorage.setItem("etoak_cms_token", resp.data.id);
                  // this.$store.state.id = resp.data.id
                  /* 调用路由前往首页 */
                  this.$router.push("/");
                } else {
                  /* 获取信息失败 */
                  this.$message({
                    message: respUser.message,
                    type: "warning",
                  });
                }
              });
            } else {
              /* 登录失败 */
              this.$message({
                message: resp.message,
                type: "warning",
              });
            }
          });
        } else {
          /* 表单验证就没通过 */
          this.$message({
            message: "登录失败！请查看验证条件",
            type: "warning",
          });
        }
      });
    },
    /* 注册 */
    register() {
      this.$router.push("/register");
    },
  },
};
</script>

<style scoped>
.login-form {
  width: 350px;
  /* 上下间隙 160px, 左右自动居中 */
  margin: 160px auto;
  background-color: rgb(255, 255, 255, 0.6);
  padding: 28px;
  border-radius: 60px;
}
.login-container {
  position: absolute;
  width: 100%;
  height: 100%;
  background: url("../../assets/login.jpg");
}
.login-title {
  color: #303133;
  text-align: center;
}
.el-input {
  width: 220px;
}
</style>
